package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.*;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.mapper.*;
import com.atguigu.lease.web.admin.service.*;
import com.atguigu.lease.web.admin.vo.attr.AttrValueVo;
import com.atguigu.lease.web.admin.vo.graph.GraphVo;
import com.atguigu.lease.web.admin.vo.room.RoomDetailVo;
import com.atguigu.lease.web.admin.vo.room.RoomItemVo;
import com.atguigu.lease.web.admin.vo.room.RoomQueryVo;
import com.atguigu.lease.web.admin.vo.room.RoomSubmitVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【room_info(房间信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class RoomInfoServiceImpl extends ServiceImpl<RoomInfoMapper, RoomInfo>
        implements RoomInfoService {

    @Autowired
    private GraphInfoService graphInfoService;
    @Autowired
    private RoomFacilityService roomFacilityService;
    @Autowired
    private RoomLabelService roomLabelService;
    @Autowired
    private RoomAttrValueService roomAttrValueService;
    @Autowired
    private RoomPaymentTypeService roomPaymentTypeService;
    @Autowired
    private RoomLeaseTermService roomLeaseTermService;
    @Autowired
    private RoomInfoMapper roomInfoMapper;

    @Autowired
    private AttrValueMapper attrValueMapper;

    @Autowired
    private FacilityInfoMapper facilityMapper;

    @Autowired
    private LabelInfoMapper labelMapper;

    @Autowired
    private PaymentTypeMapper paymentTypeMapper;

    @Autowired
    private LeaseTermMapper leaseTermMapper;

    @Autowired
    private ApartmentInfoMapper apartmentInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
//    @Autowired
//    private RoomAttrValueMapper roomAttrValueMapper;
//    @Autowired
//    private RoomFacilityMapper roomFacilityMapper;
//    @Autowired
//    private RoomPaymentTypeMapper roomPaymentTypeMapper;
//    @Autowired
//    private RoomLeaseTermMapper roomLeaseTermMapper;
//    @Autowired
//    private RoomLabelMapper roomLabelMapper;

    @Override
    public void saveOrUpdateRoom(RoomSubmitVo roomSubmitVo) {

        //1. 判断是否为更新操作
        boolean is = roomSubmitVo.getId() != null;
        //2.进行房间信息保存或者更新
        saveOrUpdate(roomSubmitVo);
        //3.如果是更新，提前删除中间表的数据
        long roomId = roomSubmitVo.getId();
        if (is){
            //删除图片-graphInfo
            LambdaQueryWrapper<GraphInfo> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(GraphInfo::getItemId,roomId);
            lambdaQueryWrapper.eq(GraphInfo::getItemType, ItemType.ROOM);
            graphInfoService.remove(lambdaQueryWrapper);
            //删除配套-roomFacility
            LambdaQueryWrapper<RoomFacility> roomFacilitylambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomFacilitylambdaQueryWrapper.eq(RoomFacility::getRoomId,roomId);
            roomFacilityService.remove(roomFacilitylambdaQueryWrapper);
            //删除标签-roomLabel
            LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,roomId);
            roomLabelService.remove(roomLabelLambdaQueryWrapper);
            //删除属性值-roomAttrValue
            LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,roomId);
            roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
            //删除支付关系-roomPaymentType
            LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,roomId);
            roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
            //删除租期关系-roomLeaseTerm
            LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
            roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,roomId);
            roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
        }
        //4.保存新的数据
        //保存新的图片-graphInfo
        List<GraphVo> graphVoList = roomSubmitVo.getGraphVoList();
        if (!CollectionUtils.isEmpty(graphVoList)){  //CollectionUtils.isEmpty()判断集合是否为空
            List<GraphInfo> graphInfoList = new ArrayList<>(graphVoList.size());
            for (GraphVo graphVo : graphVoList) {
                GraphInfo graphInfo = new GraphInfo();
                graphInfo.setName(graphVo.getName());
                graphInfo.setUrl(graphVo.getUrl());
                graphInfo.setItemType(ItemType.ROOM);
                graphInfo.setItemId(roomId);
                graphInfoList.add(graphInfo);
            }
            graphInfoService.saveBatch(graphInfoList);
        }

        //保存新的配套-roomFacility
        List<Long> roomFacilityInfoIds = roomSubmitVo.getFacilityInfoIds();
        if (!CollectionUtils.isEmpty(roomFacilityInfoIds)){
            List<RoomFacility> roomFacilityList = new ArrayList<>(roomFacilityInfoIds.size());
            for (Long roomFacilityInfoId : roomFacilityInfoIds) {
                RoomFacility roomFacility = RoomFacility.builder()
                        .facilityId(roomFacilityInfoId)
                        .roomId(roomId)
                        .build();
                roomFacilityList.add(roomFacility);
            }
            roomFacilityService.saveBatch(roomFacilityList);
        }
        //保存新的标签-roomLabel
        List<Long> roomLabelInfoIds = roomSubmitVo.getLabelInfoIds();
        if (!CollectionUtils.isEmpty(roomLabelInfoIds)){
            List<RoomLabel> roomLabelList = new ArrayList<>(roomLabelInfoIds.size());
            for (Long roomLabelInfoId : roomLabelInfoIds) {
                RoomLabel roomLabel = RoomLabel.builder()
                        .roomId(roomId)
                        .labelId(roomLabelInfoId)
                        .build();
                roomLabelList.add(roomLabel);
            }
            roomLabelService.saveBatch(roomLabelList);
        }
        //保存新的属性值-roomAttrValue
        List<Long> roomAttrValueIds = roomSubmitVo.getAttrValueIds();
        if (!CollectionUtils.isEmpty(roomAttrValueIds)){
            List<RoomAttrValue> roomAttrValueList = new ArrayList<>(roomAttrValueIds.size());
            for (Long roomAttrValueId : roomAttrValueIds) {
                RoomAttrValue roomAttrValue = RoomAttrValue.builder()
                        .roomId(roomId)
                        .attrValueId(roomAttrValueId)
                        .build();
                roomAttrValueList.add(roomAttrValue);
            }
            roomAttrValueService.saveBatch(roomAttrValueList);
        }
        //保存新的支付关系-roomPaymentType
        List<Long> roomPaymentTypeIds = roomSubmitVo.getPaymentTypeIds();
        if (!CollectionUtils.isEmpty(roomPaymentTypeIds)){
            List<RoomPaymentType> roomPaymentTypeList = new ArrayList<>(roomPaymentTypeIds.size());
            for (Long roomPaymentTypeId : roomPaymentTypeIds) {
                RoomPaymentType roomPaymentType = RoomPaymentType.builder()
                        .roomId(roomId)
                        .paymentTypeId(roomPaymentTypeId)
                        .build();
                roomPaymentTypeList.add(roomPaymentType);
            }
            roomPaymentTypeService.saveBatch(roomPaymentTypeList);
        }
        //保存新的租期关系-roomLeaseTerm
        List<Long> roomLeaseTermIds = roomSubmitVo.getLeaseTermIds();
        if(!CollectionUtils.isEmpty(roomLeaseTermIds)){
            List<RoomLeaseTerm> roomLeaseTermList = new ArrayList<>(roomLeaseTermIds.size());
            for (Long roomPaymentTypeId : roomPaymentTypeIds) {
                RoomLeaseTerm roomLeaseTerm = RoomLeaseTerm.builder()
                        .roomId(roomId)
                        .leaseTermId(roomPaymentTypeId)
                        .build();
                roomLeaseTermList.add(roomLeaseTerm);
            }
            roomLeaseTermService.saveBatch(roomLeaseTermList);
        }
    }

    @Override
    public void queryPage(Page<RoomItemVo> page, RoomQueryVo queryVo) {
        roomInfoMapper.queryPage(page,queryVo);
    }

    @Override
    public RoomDetailVo queryDetail(Long id) {
        //1.根据id 查询房间信息详情、
        RoomInfo roomInfo = roomInfoMapper.selectById(id);
        if (roomInfo == null){
            return null;
        }
        //2.查询所属公寓信息
        ApartmentInfo apartmentInfo = apartmentInfoMapper.selectById(roomInfo.getApartmentId());
        //3.查询图片列表
        List<GraphVo> graphVoList = graphInfoMapper.queryByItemId(id,ItemType.ROOM);
        //4.查询属性信息列表
        List<AttrValueVo> attrValueVoList = attrValueMapper.queryByRoomId(id);
        //5.查询配套信息列表
        List<FacilityInfo> facilityInfoList = facilityMapper.queryByRoomId(id);
        //6.查询标签信息列表
        List<LabelInfo> labelInfoList = labelMapper.queryByRoomId(id);
        //7.查询支付方式列表
        List<PaymentType> paymentTypeList = paymentTypeMapper.queryByRoomId(id);
        //8.查询可选租期列表
        List<LeaseTerm> leaseTermList = leaseTermMapper.queryByRoomId(id);
        //9.合并到 roomDetailVo 对象返回即可
        RoomDetailVo roomDetailVo = new RoomDetailVo();

        roomDetailVo.setApartmentInfo(apartmentInfo);
        roomDetailVo.setGraphVoList(graphVoList);
        roomDetailVo.setAttrValueVoList(attrValueVoList);
        roomDetailVo.setFacilityInfoList(facilityInfoList);
        roomDetailVo.setLabelInfoList(labelInfoList);
        roomDetailVo.setPaymentTypeList(paymentTypeList);
        roomDetailVo.setLeaseTermList(leaseTermList);
        //10.将原有对象的属性赋值给目标对象的相同属性！ 属性值迁移
        BeanUtils.copyProperties(roomInfo,roomDetailVo);
        return roomDetailVo;
    }

    @Override
    public void customRemoveById(Long id) {

        //1.删除房间，RoomInfo
        removeById(id);
        //2.删除图片，graphInfo
        LambdaQueryWrapper<GraphInfo> graphInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemId,id);
        graphInfoLambdaQueryWrapper.eq(GraphInfo::getItemType,ItemType.ROOM);
        graphInfoService.remove(graphInfoLambdaQueryWrapper);
        //3.删除属性值，attrValue
        LambdaQueryWrapper<RoomAttrValue> roomAttrValueLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomAttrValueLambdaQueryWrapper.eq(RoomAttrValue::getRoomId,id);
        roomAttrValueService.remove(roomAttrValueLambdaQueryWrapper);
        //4.删除配套，facilityInfo
        LambdaQueryWrapper<RoomFacility> roomFacilityLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomFacilityLambdaQueryWrapper.eq(RoomFacility::getRoomId,id);
        roomFacilityService.remove(roomFacilityLambdaQueryWrapper);
        //5.删除标签，labelInfo
        LambdaQueryWrapper<RoomLabel> roomLabelLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLabelLambdaQueryWrapper.eq(RoomLabel::getRoomId,id);
        roomLabelService.remove(roomLabelLambdaQueryWrapper);
        //6.删除支付关系，paymentType
        LambdaQueryWrapper<RoomPaymentType> roomPaymentTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomPaymentTypeLambdaQueryWrapper.eq(RoomPaymentType::getRoomId,id);
        roomPaymentTypeService.remove(roomPaymentTypeLambdaQueryWrapper);
        //7.删除租期关系，leaseTerm
        LambdaQueryWrapper<RoomLeaseTerm> roomLeaseTermLambdaQueryWrapper = new LambdaQueryWrapper<>();
        roomLeaseTermLambdaQueryWrapper.eq(RoomLeaseTerm::getRoomId,id);
        roomLeaseTermService.remove(roomLeaseTermLambdaQueryWrapper);
    }
}




